/**
 * Created by Administrator on 2017/1/16.
 */
//定义全局变量
var tytow = s('#typetwo');
var orRefresh = false;
var key = 0,requestNum = 1, areaId;

function s(select) {
    return document.querySelector(select);
}
//点击进入详情页
function toDetial(){
    var mod = document.getElementsByClassName('thetow');
    for(var i = 0; i < mod.length; i++){
        Tap(mod[i],function(e,dom){
            var chooseId = dom.getAttribute('dataid');
            window.location.href='detail.html?id='+chooseId + '&area_id=' + areaId;
        });
    }
}
//ajax模板引擎
function jshtml(data, x) {
    var mainmax = s('#mainmax');
    var html = '';
    for (var k in data) {
        var htmlm = tytow.innerHTML;
        htmlm = htmlm.replace(/\{\{imgsrc\}\}/, data[k].img);
        htmlm = htmlm.replace(/\{\{alt\}\}/, data[k].goodsname);
        htmlm = htmlm.replace(/\{\{h3\}\}/, data[k].goodsname);
        //htmlm = htmlm.replace(/\{\{h5\}\}/, "云南A级花材 uni rose");
        htmlm = htmlm.replace(/\{\{span\}\}/, data[k].pivot.promotion_price);
        htmlm = htmlm.replace(/\{\{i\}\}/, 2324);
        htmlm = htmlm.replace(/\{\{id\}\}/, data[k].id);
        html += htmlm;
    }
    if (x) {
        mainmax.children[0].innerHTML += html;
        toDetial();
        return;
    }
    var mdiv = s('#orone');
    mdiv.innerHTML = html;
    mainmax.innerHTML = '';
    mainmax.appendChild(mdiv);
    toDetial();
}

//判断首次进入是那个城市
function getCitys(){
    areaId = location.hash.substr(1);
    var citys = document.querySelectorAll('#select a');
    var title, addr, tel;
    switch (areaId){
        case 'xian':
            title = '西安';
            addr = '西安市含光北路新兴际华大厦下沉式广场03号';
            tel = '02985416061';
            citys[0].className = 'bblack';
            areaId = 311;
            break;
        case 'hangzhou':
            title = '杭州';
            addr = '杭州市滨江区聚光中心3W';
            tel = '0571-87552153';
            citys[1].className = 'bblack';
            areaId = 383;
            break;
        case 'changzhou':
            title = '常州';
            addr = '江苏省常州市天宁区青山湾花园12-1';
            tel = '0519-85338389';
            citys[2].className = 'bblack';
            areaId = 223;
            break;
    }
    s("#select_place").innerHTML = title;
    s('#special_place').innerHTML = title;
    s('#operating_address').innerHTML = addr;
    s('#operating_tel').innerHTML = tel;
}
// 切换城市
function toOtherCity() {
    var citys = document.querySelectorAll("#select a"), tel, addr;
    for(var i = 0; i < citys.length; i++){
        citys[i].index = i;
        Tap(citys[i], function (e,dom) {
            for(var j = 0; j < citys.length; j++){
                citys[j].className = '';
            }
            dom.className = 'bblack';
            s('#select_place').innerHTML = dom.children[0].innerHTML;
            s('#special_place').innerHTML = dom.children[0].innerHTML;
            areaId = dom.getAttribute('data-area_id');
            switch (Math.floor(areaId)) {
                case 311:
                    tel = '02985416061';
                    addr = '西安市含光北路新兴际华大厦下沉式广场03号';
                    break;
                case 383:
                    addr = '杭州市滨江区聚光中心3W';
                    tel = '0571-87552153';
                    break;
                case 223:
                    addr = '江苏省常州市天宁区青山湾花园12-1';
                    tel = '0519-85338389'
            }
            s('#operating_address').innerHTML = addr;
            s('#operating_tel').innerHTML = tel;
            s('#mainmax').children[0].innerHTML = '';
            ajaxRequest();
        })
    }
}
function ajaxRequest () {
    ajax({
        method: 'GET',
        url: pathName+'/api/v1/good/regionGoods/' + areaId,
        data: {
            "page":"1"
        },
        success: function (response) {
            var dark = s(".dark");
            dark.style.opacity = 1;
            Tap(dark,function(){
                dark.style.opacity = 0;
                setTimeout(function(){
                    dark.style.display = "none";
                },1000);
            });
            s(".mask").style.display = "none";
            orRefresh = true;
            requestNum++;
            var obj = JSON.parse(response).data;
            jshtml(obj, 1);
        }
    });
}
window.onload = function () {
    totop('.TOTOP');
    toOtherCity();
    getCitys();
    //页面加载完毕第一次ajax
    ajaxRequest();
    document.addEventListener("touchstart",function(){
        document.addEventListener("touchmove",function(){
            var end = document.querySelector("footer div");
            var t = document.documentElement.scrollTop || document.body.scrollTop;
            //滚动过的高度(屏幕顶到最上面··不包括现在的屏幕高度)
            var y = document.documentElement ? document.documentElement.scrollHeight : document.body.scrollHeight;
            //浏览器滚动条总高
            var w = document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight;
            //屏幕高度
            if (t > 100) {
                s('.TOTOP').style.opacity = 1;
            } else {
                s('.TOTOP').style.opacity = 0;
            }
            if (t + w > y - 200) {
                if (key == 0 && orRefresh == true) {
                    end.innerHTML = "加载中...";
                    key = 1;
                    orRefresh = false;
                    ajax({
                        method: 'GET',
                        url: pathName+'/api/v1/good/regionGoods/' + areaId,
                        data: {
                            "page":requestNum
                        },
                        success: function (response) {
                            var mask = s(".mask");
                            mask.style.display = "none";
                            var res = JSON.parse(response);
                            response = res.data;
                            requestNum = res.current_page + 1;
                            jshtml(response, 1);
                            key = 0;
                            if(res.current_page == res.last_page){
                                orRefresh = false;
                                end.innerHTML = "已经到底啦";
                            }
                        }
                    });
                }
            }
        })
    })
};